<?php
/**
 * Created by 张彪.
 * User: 小马出行
 * Date: 2017/12/11
 * Time: 15:28
 */
namespace app\api\model;

use my\Define;
use think\Db;
use think\Model;

class InvoiceModel extends Model
{
    public function getInvoiceOrderInner($map,$field='*',$flag=true,$page=1,$limit=10)
    {
        return $flag ? Db::name('order_common')->where($map)->field($field)->order('common_id desc')->page($page,$limit)->select()
            :Db::name('order_common')->where($map)->field($field)->order('common_id desc')->find();
    }

    public function countOrderInner($map)
    {
        return Db::name('order_common')->where($map)->count();
    }

    public function updateOrderInner($map,$data)
    {
        return Db::name('order_common')->where($map)->update($data);
    }

    public function getInvoiceOrderInter($map,$field='*',$flag=true,$page=1,$limit=10)
    {
        return $flag ? Db::table('inter_order_common')->where($map)->field($field)->order('id desc')->page($page,$limit)->select()
            :Db::table('inter_order_common')->where($map)->field($field)->order('id desc')->find();
    }

    public function countOrderInter($map)
    {
        return Db::table('inter_order_common')->where($map)->count();
    }

    public function updateOrderInter($map,$data)
    {
        return Db::table('inter_order_common')->where($map)->update($data);
    }

    public function searchInvoice($map,$field='*',$page=1,$limit=10)
    {
        return Db::name('invoice')->where($map)->field($field)->order('invoice_common_id desc')->page($page,$limit)->select();
    }

    public function getInvoiceInner($map,$field='a.*',$flag=true,$page=1,$limit=10)
    {
        $map = array_merge($map,array('a.invoice_type'=>1));
        return $flag ? Db::name('invoice a')->join('invoice_common b','a.invoice_common_id=b.invoice_common_id','LEFT')
            ->join('order_common c','c.common_id=a.order_common_id','LEFT')
            ->where($map)->field($field)->order('a.invoice_common_id desc')->page($page,$limit)->select()
            : Db::name('invoice a')->join('invoice_common b','a.invoice_common_id=b.invoice_common_id','LEFT')
                ->join('order_common c','c.common_id=a.order_common_id','LEFT')
                ->where($map)->field($field)->order('a.invoice_common_id desc')->select();
    }

    public function getInvoiceInter($map,$field='a.*',$flag=true,$page=1,$limit=10)
    {
        $map = array_merge($map,array('a.invoice_type'=>2));
        return $flag ?  Db::name('invoice a')->join('invoice_common b','a.invoice_common_id=b.invoice_common_id')
            ->join(config('intercity_db')['database'].'.inter_order_common c','c.id=a.order_common_id','LEFT')
            ->where($map)->field($field)->order('a.invoice_common_id desc')->page($page,$limit)->select()
            : Db::name('invoice a')->join('invoice_common b','a.invoice_common_id=b.invoice_common_id')
                ->join(config('intercity_db')['database'].'.inter_order_common c','c.id=a.order_common_id','LEFT')
                ->where($map)->field($field)->order('a.invoice_common_id desc')->select();
    }

    public function getInvoiceCommonList($map,$field='*',$page=1,$limit=10)
    {
        return Db::name('invoice_common')->where($map)->field($field)->order('invoice_common_id desc')->page($page,$limit)->select();
    }

    public function getInvoiceById($id,$field='*')
    {
        return Db::name('invoice_common')->where("invoice_common_id=$id")->field($field)->find();
    }

    public function addInvoiceCommon($data)
    {
        return Db::name('invoice_common')->insertGetId($data);
    }

    public function updateInvoiceCommon($map,$data)
    {
        return Db::name('invoice_common')->where($map)->update($data);
    }

    public function updateInvoiceInner($map,$data)
    {
        $map = array_merge($map,array('a.invoice_type'=>1));
        return Db::name('invoice a')->join('invoice_common b','a.invoice_common_id=b.invoice_common_id','LEFT')
            ->join('order_common c','c.common_id=a.order_common_id','LEFT')
            ->where($map)->update($data);
    }

    public function updateInvoiceInter($map,$data)
    {
        $map = array_merge($map,array('a.invoice_type'=>2));
        return Db::name('invoice a')->join('invoice_common b','a.invoice_common_id=b.invoice_common_id','LEFT')
            ->join(config('intercity_db')['database'].'.inter_order_common c','c.id=a.order_common_id','LEFT')
            ->where($map)->update($data);
    }

}